/**
 * 
 */
package com.gr.staffpm.security.constants;

import java.util.ArrayList;
import java.util.List;

import com.gr.staffpm.datatypes.Permission;
import com.gr.staffpm.datatypes.Project;


/**
 * @author Graham Rhodes 2 May 2011 23:36:45
 */
public enum CRUDProjectPermission {
    CreateTasks, ReadTasks, UpdateTasks, CommentOnTasks, ReadProject, UpdateProject;

    /* (non-Javadoc)
     * @see java.lang.Enum#toString()
     */
    @Override
    public String toString() {
        switch (this) {
            case CreateTasks:
                return "Create Tasks";
            case ReadTasks:
                return "Read Tasks";
            case UpdateTasks:
                return "Update Tasks";
            case CommentOnTasks:
                return "Comment On Tasks";
            case ReadProject:
                return "Read Project";
            case UpdateProject:
                return "Update Project";
        }
        return super.toString();
    }

    public static List<CRUDProjectPermission> valuesAsList() {
        List<CRUDProjectPermission> permissions = new ArrayList<CRUDProjectPermission>();
        for (CRUDProjectPermission permission : values()) {
            permissions.add(permission);
        }
        return permissions;
    }

    public String getPermissionString(Project project) {
        switch (this) {
            case CreateTasks:
                return "project:createtasks:" + project.getProjectId();
            case ReadTasks:
                return "project:readtasks:" + project.getProjectId();
            case UpdateTasks:
                return "project:updatetasks:" + project.getProjectId();
            case CommentOnTasks:
                return "project:commenttasks:" + project.getProjectId();
            case ReadProject:
                return "project:read:" + project.getProjectId();
            case UpdateProject:
                return "project:update:" + project.getProjectId();
        }
        return "";
    }

    public static CRUDProjectPermission toPermission(Permission p, Project project) {
        if (p.getPermission().endsWith(":" + project.getProjectId())) {
            if (p.getPermission().contains(":createtasks:")) {
                return CreateTasks;
            } else if (p.getPermission().contains(":readtasks:")) {
                return ReadTasks;
            } else if (p.getPermission().contains(":updatetasks:")) {
                return UpdateTasks;
            } else if (p.getPermission().contains(":commenttasks:")) {
                return CommentOnTasks;
            } else if (p.getPermission().contains(":read:")) {
                return ReadProject;
            } else if (p.getPermission().contains(":update:")) {
                return UpdateProject;
            }
        }
        return null;
    }

}
